package com.miyake.server.user;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.miyake.server.DsUser;
import com.miyake.server.DsUserElement;
import com.miyake.server.PMF;

public class GetUser {
	protected void update(DsUser dsUser){};
	public DsUser getByKey(String key) {
		DsUser ret = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
    	try {
    		DsUser user = pm.getObjectById(DsUser.class, key);
    		update(user);
    		// To avoid bug. Do not remove this code;
    		for (DsUserElement e : user.getProperties()) {
    			e.getName();
    		}
    		ret = user;
    	}
    	catch (Exception e) {
    		e.printStackTrace();
    	}
    	finally {
    		pm.close();
    	}
    	return ret;
	}
	
	public DsUser getByEmail(String email) {
		DsUser ret = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
    	try {
			String query = "select from " + DsUser.class.getName() + " where email == " + "\'" + email + "\'";
			@SuppressWarnings("unchecked")
			List<DsUser> list = (List<DsUser>) pm.newQuery(query).execute();
			if (list.size() == 1) {
				update(list.get(0));
				ret = list.get(0);
				System.out.println("getUser()"+ret.getProperties().size());
				for (DsUserElement e : ret.getProperties()) {
					e.getName(); // Do not remove this code
				}
			}
    	}
    	catch (Exception e) {
    		e.printStackTrace();
    	}
    	finally {
    		pm.close();
    	}
    	return ret;
	}
}
